package ar.com.angrymachine.hunter.activities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.Game;
import ar.com.angrymachine.hunter.GraphicStyles;
import ar.com.angrymachine.hunter.Locale;
import ar.com.angrymachine.hunter.HunterResourceManager;
import ar.com.angrymachine.hunter.gameplay.Monster;
import ar.com.angrymachine.hunter.gameplay.Spell;

public class FightActivity extends Activity
{
    public Monster monster;
    protected static final int BAR_SPLIT_GAP = 5;
    protected static final int BAR_ICON_DISTANCE_X = 3;
    protected static final int BAR_HEIGHT = 10;
    private static final int DAMAGE_ANIMATION_TIME = 200;
    private int damageTime = 0;
    
    public FightActivity(Monster monster)
    {
        this.monster = monster;
    }
    
    public String getDescription()
    {
        return Locale.getMonsterDescription(this.monster);
    }
    
    public void receiveSpell(Spell spell)
    {
        monster.receiveSpell(spell);
    }
    
    public String getActionText()
    {
        if(isCompleted())
        {
            return Locale.VICTORY;
        }
        else
        {
            if(isRunning())
            {
                return Locale.ATTACKING;
            }
            else
            {
                return Locale.ATTACK;
            }
        }
    }
    
    public boolean isCompleted()
    {
        return monster.isDead();
    }
    
    protected void die()
    {
    	end(true);
    }

    public void update(int delta, boolean action, boolean timeIsRunning)
    {
        if(timeIsRunning)
        {
            if(action)
            {
                monster.receiveAttack(Game.current.player.getHitDamage());
                if(monster.isDead())
                {
                    die();
                }
            }
            // attack
            if(monster.update(delta))
            {
            	damageTime = DAMAGE_ANIMATION_TIME;
            }
            
        }
        damageTime -= delta;
        if(damageTime < 0)
        {
        	damageTime = 0;
        }

    }
    
    public void paint(Graphics g, Rectangle activityArea)
    {
    	if(damageTime > 0 && !isCompleted())
    	{
    		GraphicStyles.paintDefaultActivity(g, this, activityArea, monster.icon, GraphicStyles.slumberBarColor());
    	}
    	else
    	{
    		GraphicStyles.paintDefaultActivity(g, this, activityArea, monster.icon);
    	}
        if(!isCompleted())
        {
        	Rectangle[] bars = GraphicStyles.getBarAreas(activityArea, 2);
            Image healthIcon = HunterResourceManager.getImage("heart-full.png");
            Image attackIcon = HunterResourceManager.getImage("attack.png");
        	int iconsWidth = Math.max(healthIcon.getWidth(null), attackIcon.getWidth(null));
        	// attack
            g.setColor(Color.YELLOW);
            GraphicStyles.paintIconizedBar(g, attackIcon, iconsWidth, bars[0], monster.timeToAttack, (int)monster.attackRate);
            // health
            g.setColor(Color.RED);
            GraphicStyles.paintIconizedBar(g, healthIcon, iconsWidth, bars[1], (int)monster.getHealth(), (int)monster.getMaxHealth());
        }
    }

    public void onFinished()
    {
        // drop treasure
        if(monster.treasure != null)
        {
            Game.current.currentLevel.addActivity(this, new TreasureActivity(monster.treasure));
        }
    }
}
